﻿package
{
	import flash.display.*;
	import flash.events.MouseEvent;
	import fl.data.DataProvider;
	import fl.controls.ComboBox;
	import fl.controls.CheckBox;
	import fl.controls.Button;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import flash.events.*;
	import fl.events.DataChangeEvent;
	import flash.desktop.NativeApplication;
	
	public class TimerSelect extends MovieClip
	{
		public static var timer_list:DataGrid;
		public static var dateSelect:ComboBox;
		public static var show_deleted:CheckBox;

		public var dates:DataProvider;
		public var timers:DataProvider;

		public static var myParent:MovieClip;
		
		public function TimerSelect()
		{
			timer_list = TimerList;
			show_deleted = ShowDeleted;

			dates = new DataProvider();
			timers = new DataProvider();
			
			dateSelect = new ComboBox();
			dateSelect.x = 11;
			dateSelect.y = 31;
			dateSelect.width = 482;
			dateSelect.dataProvider = dates;
			dateSelect.prompt = 'Select a log date';
			dateSelect.rowCount = 15;
			this.addChild(dateSelect);
			
			myParent = NativeApplication.nativeApplication.openedWindows[0].stage.getChildAt(0);
			
			timer_list.dataProvider = timers;
			dateSelect.dataProvider = dates;
			
			var task_number:DataGridColumn = new DataGridColumn('task_number');
			//task_number.cellRenderer = '#';
			task_number.headerText = '#';
			task_number.width = 50;
			timer_list.addColumn(task_number);
			
			var description:DataGridColumn = new DataGridColumn('description');
			description.headerText = 'Title';
			description.width = 420;
			timer_list.addColumn(description);
			
			var Time:DataGridColumn = new DataGridColumn('timeDisplay');
			Time.headerText = 'Time';
			timer_list.addColumn(Time);
			
			//dates.addEventListener(DataChangeEvent.DATA_CHANGE, updateTaskColors, false, 0, true);
			
			dateSelect.addEventListener(Event.CHANGE, change_date, false, 0, true);
			show_deleted.addEventListener(Event.CHANGE, change_date, false, 0, true);

			this.dragBar.addEventListener(MouseEvent.MOUSE_DOWN, drag_me, false, 0, true);
			this.cancel_button.addEventListener(MouseEvent.CLICK, hideSettings, false, 0, true);
			this.select_button.addEventListener(MouseEvent.CLICK, selectTimer, false, 0, true);

			get_dates()
		}

		function get_dates()
		{
			var timerLog:TimerLog = new TimerLog();
			var logDates = timerLog.get_all_dates();


			var timerDate:Date;
			for each(var d in logDates)
			{
				timerDate = new Date();
				timerDate.setTime(d.date);
				if(timerDate.toLocaleDateString() != 'Invalid Date')
				{
					dates.addItem({
						label:timerDate.toLocaleDateString(),
						timestamp:d.date
					});
				}
				else
				{
					timerDate = new Date();
					timerDate.setTime(Date.parse(d.date));
					dates.addItem({
						label:timerDate.toLocaleDateString(),
						timestamp:d.date
					});
				}
			}
		}

		function updateTaskColors(e:DataChangeEvent)
		{
			Tools.backtrace();
			var _data = e.target.toArray();
			Tools.pr(_data);
			if(_data[0])
			{
				//task_list.setStyle("CellRenderer", TaskColor);
			}
		}

		function change_date(e:Event):void
		{
			if(dateSelect.selectedItem)
			{
				timers.removeAll();
				var timerLog:TimerLog = new TimerLog();
				var deleted = '';
				if(!show_deleted.selected)
				{
					deleted = ' And deleted=0';
				}
				var logTimers = timerLog.get_timer("Where date='"+dateSelect.selectedItem.timestamp+"'"+deleted);
				for each(var t in logTimers)
				{
					t.timeDisplay = Tools.formatTime(t.time);
					timers.addItem(t);
				}
			}
		}

		function change_person(e:Event):void
		{
			var url_params:Object = new Object();
			var current_person = e.target.selectedItem;
			url_params.assigneeid = current_person.id;
			
			Intervals.get_tasks(url_params);
		}
		
		function hideSettings(e)
		{
			this.parent.removeChild(e.target.parent);
		}
		
		function selectTimer(e:MouseEvent):void
		{
			if(timer_list.selectedItems)
			{
//				var task:Tasks = new Tasks();
//				var selected_task = Intervals.get_task(task_list.selectedItem.
//				task.set_details(taskXML);
//				task.insert();
				for each(var t in timer_list.selectedItems)
				{
					Main.new_timer(t.timer_id, t.time, t.task_number, t.description);
				}
				this.parent.removeChild(e.target.parent);
			}
		}

		function drag_me(e:MouseEvent)
		{
			stage.nativeWindow.startMove();
		}

		public static function get_myParent()
		{
			return myParent;// = p;
		}
	}
}